<template>
  <transition name="slide">
    <div class="add-song" @click.stop v-show="showFlag">
      <div class="header">
        <h1 class="title">添加歌曲到列表</h1>
        <div class="close" @click="hide">
          <i class="iconfont icon-delete"></i>
        </div>
      </div>
      <div class="search-box-wrapper">
        <search-box ref="searchBox" @query="onQueryChange" placeholder="搜索歌曲" />
      </div>
      <div class="shortcut" v-show="!query">
        <switches :switches="switches" :currentIndex="currentIndex" @switch="switchItem" />
        <div class="list-wrapper">
          <scroll ref="songListScroll"
            :refreshDelay="refreshDelay"
            class="list-scroll"
            v-if="currentIndex === 0" :data="playHistory"
          >
            <div class="list-inner">
              <song-list :songs="playHistory" @select="selectSong" />
            </div>
          </scroll>
          <scroll ref="searchListScroll"
            :refreshDelay="refreshDelay"
            class="list-scroll"
            v-if="currentIndex === 1" :data="searchHistory"
          >
            <div class="list-inner">
              <search-list @delete="deleteSearchHistory" @select="addQuery" :searches="searchHistory" />
            </div>
          </scroll>
        </div>
      </div>
      <div class="search-result" v-show="query">
        <suggest :query="query" @select="selectSuggest" @listScroll="blurInput" />
      </div>
      <top-tip ref="topTip">
        <div class="tip-title">
          <i class="iconfont icon-trophy"></i>
          <span class="text">1首歌曲已经添加到播放队列</span>
        </div>
      </top-tip>
    </div>
  </transition>
</template>

<script>
import SearchBox from '../../base/search-box/SearchBox'
import Suggest from '../suggest/Suggest'
import Switches from '../../base/switches/Switches'
import Scroll from '../../base/scroll/Scroll'
import SongList from '../../base/song-list/SongList'
import SearchList from '../../base/search-list/SearchList'
import TopTip from '../../base/top-tip/TopTip'
import { searchMixin } from '../../assets/js/mixin'
import { mapGetters, mapActions } from 'vuex'
import Song from '../../assets/js/song'

export default {
  name: 'AddSong',
  mixins: [searchMixin],
  data() {
    return {
      showFlag: false,
      currentIndex: 0,
      switches: [
        { name: '最近播放' },
        { name: '搜索历史' }
      ]
      // query: ''
    }
  },
  computed: {
    ...mapGetters([
      'playHistory'
    ])
  },
  methods: {
    show() {
      this.showFlag = true
      setTimeout(() => {
        if (this.currentIndex === 0) {
          this.$refs.songListScroll.refresh()
        } else {
          this.$refs.searchListScroll.refresh()
        }
      }, 20)
    },
    hide() {
      this.showFlag = false
    },
    selectSuggest() {
      this.saveSearch()
      this.showTip()
    },
    switchItem(index) {
      this.currentIndex = index
    },
    selectSong(song, index) {
      if (index !== 0) {
        // 这里注意: 这个song不是song实例, 我们是从缓存里面取的, 虽然song的属性都有
        this.insertSong(new Song(song))
        this.showTip()
      }
    },
    showTip() {
      this.$refs.topTip.show()
    },
    ...mapActions([
      'insertSong'
    ])
  },
  components: {
    SearchBox,
    Suggest,
    Switches,
    Scroll,
    SongList,
    SearchList,
    TopTip
  }
}
</script>

<style lang="scss" scoped>
@import "../../assets/css/variable.scss";
@import "../../assets/css/mixin.scss";

.add-song{
  position: fixed;
  top: 0;
  bottom: 0;
  width: 100%;
  z-index: 200;
  background: $color-background;
  &.slide-enter-active, &.slide-leave-active{
    transition: all 0.3;
  }
  &.slide-enter, &.slide-leave-to{
    transform: translate3d(100%, 0, 0);
  }
  .header{
    position: relative;
    height: 44px;
    text-align: center;
    .title{
      line-height: 44px;
      font-size: $font-size-large;
      color: $color-text;
    }
    .close{
      position: absolute;
      top: 0;
      right: 8px;
      .icon-delete{
        display: block;
        padding: 12px;
        font-size: 20px;
        color: $color-theme;
      }
    }
  }
  .search-box-wrapper{
    margin: 20px;
  }
  .shortcut{
    .list-wrapper{
      position: absolute;
      top: 165px;
      bottom: 0;
      width: 100%;
      .list-scroll{
        height: 100%;
        overflow: hidden;
        .list-inner{
          padding: 20px 30px;
        }
      }
    }
  }
  .search-result{
    position: fixed;
    top: 124px;
    bottom: 0;
    width: 100%;
  }
  .tip-title{
    text-align: center;
    padding: 18px 0;
    font-size: 0;
    .icon-ok{
      font-size: $font-size-medium;
      color: $color-theme;
      margin-right: 4px;
    }
    .text{
      font-size: $font-size-medium;
      color: $color-text;
    }
  }
}
</style>